<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/23 0023
 * Time: 上午 11:15
 */


namespace app\admin\controller;

use think\Db;
use think\Request;
use app\common\model\Base;


class Record extends  Basecontroller
{

    /**
     * 记录列表
     *
     */
    public function index(){

        //获取搜索出来的患者列表
        \think\Loader::action('doctor/Record/index','event');

        $this->assign('breadcrumb', '记录列表');
        return $this->fetch();
    }


    /**
     * 查看详情页面
    **/

    public function check_detail(){

        //获取搜索出来的患者列表
        \think\Loader::action('doctor/Record/check_detail','event');
        $this->assign('breadcrumb', '查看详情');
        return $this->fetch();
    }

    /**
     * 添加记录
     **/

    public function add_record(){

        //获取搜索出来的患者列表
        $data = $this->request->param();

        $patientInfo = $this->getPatient_ById($data['id']);
        $this->assign("patient_info",$patientInfo);

        //获取检查类型
        $option['is_deleted'] = 1;
        $option['type'] = 1;
        $case_select = $this->getCase_type($option);
        $this->assign("case_select",$case_select);
        //获取维度/部位
        $option['is_deleted'] = 1;
        $option['type'] = 2;
        $case_depart = $this->getCase_type($option);
        $this->assign("case_depart",$case_depart);
        $option['is_deleted'] = 1;
        $option['type'] = 3;
        $case_lat = $this->getCase_type($option);
        $this->assign("case_lat",$case_lat);


        $this->assign('breadcrumb', '添加记录');
        return $this->fetch();
    }

    /**
     * 添加记录
     **/

    public function edit_record(){

        //获取搜索出来的患者列表
        $data = $this->request->param();
        $id = $data['id'];
        $patientcase_info = $this->getPatientcase_info($id);
        $patient_part_lat = json_decode($patientcase_info['patient_part_lat'],true);
        $patient_part_lat_1 = $patient_part_lat[1];

        $lats = [];
        $parts = [];
        unset($patient_part_lat[1]);
        foreach($patient_part_lat as $val){
            $lats[] = $val['lat'];
            $parts[] = $val['part'];
        }

       // print_r($patient_part_lat);
        $this->assign('patient_part_lat', $patient_part_lat);
        $this->assign('patient_part_lat_1', $patient_part_lat_1);
        $this->assign('lats', $lats);
        $this->assign('parts', $parts);

        $patient_info = $this->getPatient_ById($patientcase_info['patient_id']);


        $this->assign('patientcase_info', $patientcase_info);
        $this->assign('patient_info', $patient_info);


        //取出电子报告
        $this->getPatient_report($id);
        //取出电子胶片
        $this->getPatient_film($id);



        //获取检查类型
        $option['is_deleted'] = 1;
        $option['type'] = 1;
        $case_select = $this->getCase_type($option);
        $this->assign("case_select",$case_select);
        //获取维度/部位
        $option['is_deleted'] = 1;
        $option['type'] = 2;
        $case_depart = $this->getCase_type($option);
        $this->assign("case_depart",$case_depart);
        $option['is_deleted'] = 1;
        $option['type'] = 3;
        $case_lat = $this->getCase_type($option);
        $this->assign("case_lat",$case_lat);





        $this->assign('breadcrumb', '编辑记录');
        return $this->fetch('add_record');
    }




    /**
     *获取添加分组dcm图片的列表
     **/
    public function add_record_depart(){


        $num = $this->request->param('num');
        $num = $num+1;

        $html = $this->uploadDcmByJs($num);
        return $html;
    }


    /**
     * 图片上传
     **/
    public function uploadFile(){

        $filename = "pic";
        $poster_url = $_FILES[$filename]["tmp_name"];
        $tempfilename = explode(".",$_FILES[$filename]["name"]);

        $reinfo = uploadimg($poster_url,end($tempfilename));

        return json($reinfo);
    }

    /**
     * 导出记录数据
    **/
    public function get_xls_from_record(){
        $option['patient_case.is_deleted'] = ['=', 1];

        $list = model('patientcase')
            ->field('patient_case.case_num,patient_case.patient_part_lat,patient_case.check_time,patient_case.check_result,patient_case_type.name as patient_case_type_name,patient.name as patient_name,patient.sex,patient.document_num,patient.birth,doctor.name as doctor_name,doctor_department.department as department_name')
            ->join('patient','patient.id=patient_case.patient_id')
            ->join('doctor','doctor.id=patient_case.doctor_id')
            ->join('doctor_department','doctor_department.id=doctor.depart_id')
            ->join('patient_case_type','patient_case_type.id=patient_case.patient_case_type')
            ->where($option)
            ->order('patient_case.create_time desc')
            ->select();

        //获取数据);

        require "./extend/PHPExcel/PHPExcel.php";
        require "./extend/PHPExcel/PHPExcel/IOFactory.php";
        require "./extend/PHPExcel/PHPExcel/Style/Fill.php";
        $excel = new \PHPExcel();

        $name = '患者记录列表';
        $excel->setActiveSheetIndex(0)->setCellValue("A1", "检查编号")
            ->setCellValue("B1", "姓名")
            ->setCellValue("C1", "性别")
            ->setCellValue("D1", "身份证号")
            ->setCellValue("E1", "年龄")
            ->setCellValue("F1", "类型")
            ->setCellValue("G1", "检查结果")
            ->setCellValue("H1", "部位")
            ->setCellValue("I1", "检查时间")
            ->setCellValue("J1", "所属部门")
            ->setCellValue("K1", "录入医生");

        $i =2;
        foreach($list as $key=>$v){
            $patient_part_lat = json_decode($v['patient_part_lat'],true);
            $sex = "女";
            //print_r($v['sex']);
            if($v['sex'] == 1){
                $sex = "男";
            }
            $string = '';
            $string_part_lat = '';
            foreach($patient_part_lat as $sonVal){
                $part = isset($sonVal['part']) ? $sonVal['part'] : '';
                $lat = isset($sonVal['lat']) ? $sonVal['lat'] : '';
                $string_part_lat = $part."/".$lat;
                $string .= $string_part_lat."、";
            }

            $excel->setActiveSheetIndex(0)->setCellValue("A" . $i, $v['case_num'])
                ->setCellValue("B" . $i, $v['patient_name'])
                ->setCellValue("C" . $i, $sex)
                ->setCellValue("D" . $i, $v['document_num'])
                ->setCellValue("E" . $i, getAge($v['birth']))
                ->setCellValue("F" . $i, $v['patient_case_type_name'])
                ->setCellValue("G" . $i, $v['check_result'])
                ->setCellValue("H" . $i, $string)
                ->setCellValue("I" . $i, $v['check_time'])
                ->setCellValue("J" . $i, $v['department_name'])
                ->setCellValue("K" . $i, $v['doctor_name']);
            $i++;
        }
        ob_end_clean();
        ob_start();
        // 生成2003excel格式的xls文件
        header('Content-Type: application/vnd.ms-excel;charset=utf-8');
        header('Content-Disposition: attachment;filename="' . $name . '.xls"');
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
        $objWriter->save('php://output');
        exit;

    }

}